"""
Case Type   : cm_ctl
Case Name   : 执行多次cm_ctl resume命令
Create At   : 2023/03/24
Owner       : opentestcase012
Description :
    1.所有节点下查看暂停标志文件;
    2.执行cm_ctl pause;
    3.再次查看暂停标志文件;
    4.执行cm_ctl resume;
    5.再次查看暂停标志文件;
    6.再次执行cm_ctl resume;
    7.再次查看暂停标志文件;
Expect      :
    1.无暂停标志文件
    2.执行成功
    3.生成暂停标志文件
    4.执行成功
    5.暂停标志文件被删除
    6.执行成功
    7.暂停标志文件不存在
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

primary_sh = CommonSH('PrimaryDbUser')


# @unittest.skipIf(2 != primary_sh.get_node_num(), '非1+2环境不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.pause_file = os.path.join(macro.GAUSSHOME_BIN_PATH,
                                       'cluster_manual_pause')

    def test_cm(self):
        text = '----step1:所有节点下查看暂停标志文件; expect:无暂停标志文件----'
        self.log.info(text)
        res = self.primary_sh.exec_gs_ssh(f'ls {self.pause_file}')
        self.assertEqual(3, res.count('No such file or directory'),
                         "执行失败:" + text)

        text = '----step2:执行cm_ctl pause; expect:执行成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('pause')
        self.assertIn(self.constant.cm_pause_success_msg, cm_ctl_res,
                      '执行失败:' + text)

        text = '----step3:再次查看暂停标志文件; expect:生成暂停标志文件----'
        self.log.info(text)
        res = self.primary_sh.exec_gs_ssh(f'ls {self.pause_file}')
        self.assertEqual(3, res.count(self.pause_file), "执行失败:" + text)

        text = '----step4:执行cm_ctl resume; expect:执行成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('resume')
        self.assertIn(self.constant.cm_resume_success_msg, cm_ctl_res,
                      '执行失败:' + text)

        text = '----step5:再次查看暂停标志文件; expect:暂停标志文件被删除----'
        self.log.info(text)
        res = self.primary_sh.exec_gs_ssh(f'ls {self.pause_file}')
        self.assertEqual(3, res.count('No such file or directory'),
                         "执行失败:" + text)

        text = '----step6:再次执行cm_ctl resume; expect:执行成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('resume')
        self.assertIn(self.constant.cm_resume_success_msg, cm_ctl_res,
                      '执行失败:' + text)

        text = '----step7:再次查看暂停标志文件; expect:暂停标志文件被删除----'
        self.log.info(text)
        res = self.primary_sh.exec_gs_ssh(f'ls {self.pause_file}')
        self.assertEqual(3, res.count('No such file or directory'),
                         "执行失败:" + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text = '----step8:恢复集群状态; expect:执行成功----'
        self.log.info(text)
        cm_ctl_res1 = self.primary_sh.exec_cm_ctl('resume')
        self.assertIn(self.constant.cm_resume_success_msg, cm_ctl_res1,
                      '执行失败:' + text)
        self.log.info(f'----{os.path.basename(__file__)} finish----')
